package com.lge.remoteshot;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.lge.remoteshot.camera.CameraActivity;

public class DragSelectView extends View{
	Bitmap bitmapLock, bitmapLockBig, bitmapArrowLeft, bitmapArrowRight;
	Bitmap bitmapRemote, bitmapRemoteBig, bitmapCamera, bitmapCameraBig;
	Bitmap bitmapSelect, bitmapLeftChoice, bitmapRightChoice;
	int upX, upY;
	int dragX, dragY; // touch point
	int downX, downY;
	int viewWidth, viewHeight; // view scale
	int nBitmapLockWidth, nBitmapLockHeight; //lock image scale
	int midX, midY; //center
	int leftX, leftY;
	int rightX, rightY;
	int dotX;

	Matrix matrix;
	
	boolean bIsInit = true;
	boolean bNowSelectChange = false;
	boolean bNowCanSelect = false;
	int nCaseDrawCircle = 1; // 0 is left select, 1 is no select , 2 is right select
	Context mContext;
	
	public DragSelectView(Context context){
		super(context);
		init();
		mContext = context;
	}
	public DragSelectView(Context context, AttributeSet set){
		super(context, set);
		init();
		mContext = context;
	}
	
	public void init(){
		bitmapLock = BitmapFactory.decodeResource(getResources(), R.drawable.mobile);
		//bitmapLockBig = BitmapFactory.decodeResource(getResources(), R.drawable.mobile);
		bitmapCamera = BitmapFactory.decodeResource(getResources(), R.drawable.camera);
		bitmapCameraBig = BitmapFactory.decodeResource(getResources(), R.drawable.camera_up);
		bitmapRemote = BitmapFactory.decodeResource(getResources(), R.drawable.remote);
		bitmapRemoteBig = BitmapFactory.decodeResource(getResources(), R.drawable.remote_up);
		bitmapArrowLeft = BitmapFactory.decodeResource(getResources(), R.drawable.arrow_left);
		bitmapArrowRight = BitmapFactory.decodeResource(getResources(), R.drawable.arrow_right);
		bitmapSelect = bitmapLock;
		bitmapLeftChoice = bitmapCamera;
		bitmapRightChoice = bitmapRemote;
		matrix = new Matrix();
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event){
		int nAction = event.getAction();
		
		switch(nAction){
			case MotionEvent.ACTION_DOWN:
				downX = (int)event.getX();
				downY = (int)event.getY();
				if((downX  > midX-viewWidth/10) && (downX < midX+viewWidth/10) &&
						(downY  > midY-viewHeight/10) && (downY < midY+viewHeight/10))
					bNowSelectChange = true;
				break;
			case MotionEvent.ACTION_MOVE:
				if(bNowSelectChange){
					dragX = (int)event.getX();
					dragY = (int)event.getY();
					if(dragX < midX){
						bitmapSelect = bitmapArrowLeft;
						bitmapLeftChoice = bitmapCameraBig;
						bitmapRightChoice = bitmapRemote;
					}
					else if(dragX > midX){
						bitmapSelect = bitmapArrowRight;
						bitmapLeftChoice = bitmapCamera;
						bitmapRightChoice = bitmapRemoteBig;
					}
					else{
						bitmapSelect = bitmapLock;
						bitmapLeftChoice = bitmapCamera;
						bitmapRightChoice = bitmapRemote;
					}
					
					if(dragX < leftX + (viewWidth/10)*2){
						//select left case
						bNowCanSelect = true;
						nCaseDrawCircle = 0;
					}
					else if(dragX > rightX - (viewWidth/10)* 2){
						//select right case
						bNowCanSelect = true;
						nCaseDrawCircle = 2;
					}
					else{
						bNowCanSelect = false;
						nCaseDrawCircle = 1;
					}
				}
				break;
			case MotionEvent.ACTION_UP:
				if(bNowSelectChange){
					upX = (int)event.getX();
					upY = (int)event.getY();
					dragX = viewWidth/2;
					dragY = viewHeight/2;
					
					if(upX < leftX + (viewWidth/10)*2){
						//select left case
						Intent intent = new Intent(mContext, CameraActivity.class);
						intent.putExtra("mode", CameraActivity.CAMERA_MODE_ORIGINAL);
						mContext.startActivity(intent);
					}
					else if(upX > rightX - (viewWidth/10)* 2){
						//select right case
						Intent intent = new Intent(mContext, CameraActivity.class);
						intent.putExtra("mode", CameraActivity.CAMERA_MODE_REMOTE);
						mContext.startActivity(intent);
					}
					nCaseDrawCircle = 1;
					bitmapSelect = bitmapLock;
					bitmapLeftChoice = bitmapCamera;
					bitmapRightChoice = bitmapRemote;
					bNowSelectChange = false;
					bNowCanSelect = false;
				}
				break;
		}
		invalidate();
		return true;
	}
	
	@Override
	protected void onDraw(Canvas canvas){
		if(bIsInit){
			viewWidth = getWidth();
			viewHeight = getHeight();
			midX = getWidth() / 2;
			midY = getHeight() / 2;
			leftX = (getWidth() / 10);
			rightX = (getWidth() / 10) * 9;
			nBitmapLockWidth = bitmapLock.getWidth();
			nBitmapLockHeight = bitmapLock.getHeight();
			dragX = midX;
			bIsInit = false;
		}
		Paint paint = new Paint();
		canvas.drawColor(0x00000000);
		paint.setFilterBitmap(true);
		paint.setAntiAlias(true);
		
		paint.setStyle(Paint.Style.STROKE);
		paint.setStrokeWidth(2);
		paint.setColor(Color.WHITE);
		
		if(!bNowSelectChange)
			canvas.drawCircle(midX, midY, nBitmapLockWidth, paint);
		
		if(nCaseDrawCircle == 0)
			canvas.drawCircle(leftX, midY, nBitmapLockWidth, paint);
		else if(nCaseDrawCircle == 2)
			canvas.drawCircle(rightX, midY, nBitmapLockWidth, paint);
		
		canvas.drawBitmap(bitmapLeftChoice, leftX - nBitmapLockWidth/2, midY - nBitmapLockHeight/2, null);
		canvas.drawBitmap(bitmapRightChoice, rightX - nBitmapLockWidth/2, midY - nBitmapLockHeight/2, null);

		paint.setStyle(Paint.Style.FILL);
		paint.setColor(Color.LTGRAY);
		if(dragX < midX){
			dotX = midX;
			while(dotX > dragX){
				canvas.drawCircle(dotX, midY, 5, paint);
				dotX -= viewWidth/20;
			}
		}
		else{
			dotX = midX;
			while(dotX < dragX){
				canvas.drawCircle(dotX, midY, 5, paint);
				dotX += viewWidth/20;
			}
		}

		if(!bNowCanSelect)
			canvas.drawBitmap(bitmapSelect, dragX - nBitmapLockWidth/2, midY - nBitmapLockHeight/2, null);
	}
}